package com.dmgis.hndz.controller;

import com.dmgis.hndz.common.result.HttpStatusEnum;
import com.dmgis.hndz.common.result.ResponseResult;
import com.dmgis.hndz.common.utils.ExcelUtil;
import com.dmgis.hndz.common.utils.StringUtil;
import com.dmgis.hndz.common.utils.SysResult;
import com.dmgis.hndz.pojo.SMSRecordManagement;
import com.dmgis.hndz.service.SMSRecordManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author pengdongdong
 * @description public
 * @date 2024-11-11
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/sMSRecordManagement")
public class SMSRecordManagementController {

    @Autowired
    private SMSRecordManagementService sMSRecordManagementService;

    /**
     * 新增
     *
     * @author pengdongdong
     * @date 2024/11/11
     **/
    @PostMapping("/save")
    public SysResult insertSMSRecordManagement(@RequestBody SMSRecordManagement sMSRecordManagement) {
        if (CollectionUtils.isEmpty(sMSRecordManagement.getReceiver())) {
            return SysResult.fail("短信接收人和手机号不能为空！");
        }
        if (StringUtil.IsNullOrEmpty(sMSRecordManagement.getContent())) {
            return SysResult.fail("短信内容不能为空！");
        }
        return sMSRecordManagementService.insertSMSRecordManagement(sMSRecordManagement);
    }

    /**
     * 刪除
     *
     * @author pengdongdong
     * @date 2024/11/11
     **/
    @PostMapping("/delete")
    public SysResult deleteSMSRecordManagement(String ids) {
        return SysResult.success(sMSRecordManagementService.deleteSMSRecordManagement(ids));
    }

    /**
     * 更新
     *
     * @author pengdongdong
     * @date 2024/11/11
     **/
    @PostMapping("/update")
    public SysResult updateSMSRecordManagement(@RequestBody SMSRecordManagement sMSRecordManagement) {
        return SysResult.success(sMSRecordManagementService.updateSMSRecordManagement(sMSRecordManagement));
    }

    /**
     * 查询 根据主键 id 查询
     *
     * @author pengdongdong
     * @date 2024/11/11
     **/
    @GetMapping("/getById")
    public ResponseResult selectSMSRecordManagementById(String id) {
        return new ResponseResult(HttpStatusEnum.SUCCESS.getCode(), true, "查询成功", sMSRecordManagementService.selectSMSRecordManagementById(id));
    }

    /**
     * 查询 分页查询
     *
     * @author pengdongdong
     * @date 2024/11/11
     **/
    @GetMapping("/list")
    public SysResult selectSMSRecordManagementList(Integer pageNum, Integer pageSize, SMSRecordManagement sMSRecordManagement) {
        return SysResult.success(sMSRecordManagementService.selectSMSRecordManagementList(pageNum, pageSize, sMSRecordManagement));
    }

    /**
     * 导出短信信息
     *
     * @param response
     */
    @PostMapping("/excelSMS")
    public void excelSMS(SMSRecordManagement sMSRecordManagement, HttpServletResponse response) throws IOException {
        List<SMSRecordManagement> list = sMSRecordManagementService.selectSMSRecordManagement(sMSRecordManagement);
        ExcelUtil<SMSRecordManagement> excelUtil = new ExcelUtil<>(SMSRecordManagement.class);
        excelUtil.exportExcel(response, list, "SMSRecordManagement", "sms");
    }

    /**
     * 信息呼叫统计
     *
     * @param dateType  1：本年，2：本月，3：本日 4：本周
     * @param code      行政编码
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/statisticsCallSMS")
    public SysResult statisticsCallSMS(Integer dateType, String code, String startTime, String endTime) {
        if (StringUtil.IsNullOrEmpty(code)) {
            return SysResult.fail("行政编码不能为空！");
        }
        return SysResult.success(sMSRecordManagementService.statisticsCallSMS(dateType, code, startTime, endTime));
    }

    /**
     * 统计市县级呼叫人数
     *
     * @param code      行政编码
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/selectCallNum")
    public SysResult selectCallNum(String code, String startTime, String endTime) {
        if (StringUtil.IsNullOrEmpty(code)) {
            return SysResult.fail("行政编码不能为空！");
        }
        return SysResult.success(sMSRecordManagementService.selectCallNum(code, startTime, endTime));
    }

}